package com.atguigu.eduservice.controller;


import com.atguigu.commonutils.Result;
import com.atguigu.eduservice.entity.EduTeacher;
import com.atguigu.eduservice.entity.vo.TeacherQueryVO;
import com.atguigu.eduservice.service.EduTeacherService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author zr
 * @since 2021-11-05
 */
@Api(tags = "讲师管理")
@RestController
//@CrossOrigin
@RequestMapping("/edu/service/teacher")
public class EduTeacherController {
    @Resource
    private EduTeacherService eduTeacherService;

    /**
     * 查询讲师列表数据
     *
     * @return {@link List}<{@link EduTeacher}>
     */
    @ApiOperation(value = "所有讲师列表")
    @GetMapping("findAll")
    public Result findAllTeacher() {
        List<EduTeacher> list = eduTeacherService.list(null);
        return Result.ok(list);
    }

    /**
     * 删除老师
     *
     * @param id id
     * @return {@link Boolean}
     */
    @ApiOperation(value = "根据ID删除讲师")
    @DeleteMapping("delete/{id}")
    public Result removeTeacher(
            @ApiParam(name = "id", value = "讲师ID", required = true)
            @PathVariable String id) {
        boolean flag = eduTeacherService.removeById(id);
        return flag ? Result.ok() : Result.fail();
    }

    @ApiOperation(value = "分页讲师列表")
    @GetMapping("pageListTeacher/{current}/{limit}")
    public Result pageTeacher(@ApiParam(name = "current", value = "当前页码", required = true)
                              @PathVariable long current,
                              @ApiParam(name = "limit", value = "每页记录数", required = true)
                              @PathVariable long limit) {
        Page<EduTeacher> eduTeacherPage = new Page<>(current, limit);
        eduTeacherService.page(eduTeacherPage, null);
        return Result.ok(eduTeacherPage);
    }

    @ApiOperation(value = "按条件查询")
    @PostMapping("pageTeacherCondition/{current}/{limit}")
    public Result pageTeacherCondition(@ApiParam(name = "current", value = "当前页码", required = true)
                                       @PathVariable long current,
                                       @ApiParam(name = "limit", value = "每页记录数", required = true)
                                       @PathVariable long limit,
                                       @RequestBody TeacherQueryVO teacherQueryVO) {
        // 创建page对象，传递当前页，每页记录数
        Page<EduTeacher> teacherPage = new Page<>(current, limit);
        // 构造查询条件
        QueryWrapper<EduTeacher> queryWrapper = new QueryWrapper<>();
        // 多条件组合查询
        String name = teacherQueryVO.getName();
        Integer level = teacherQueryVO.getLevel();
        String begin = teacherQueryVO.getBegin();
        String end = teacherQueryVO.getEnd();
        // 判断条件值是否为空  不为空才添加条件
        // 这里使用了hasText以及hasLength  spring5的isEmpty方法已经被弃用
        if (StringUtils.hasText(name)) {
            queryWrapper.like("name", name);
        }
        if (!StringUtils.isEmpty(level)) {
            queryWrapper.eq("level", level);
        }
        if (StringUtils.hasLength(begin)) {
            queryWrapper.ge("gmt_create", begin);
        }
        if (StringUtils.hasLength(end)) {
            queryWrapper.le("gmt_modified", end);
        }
        // 排序
        queryWrapper.orderByDesc("gmt_create");
        eduTeacherService.page(teacherPage, queryWrapper);
        return Result.ok(teacherPage);
    }

    @ApiOperation(value = "新增讲师")
    @PostMapping("addTeacher")
    public Result addTeacher(@RequestBody EduTeacher eduTeacher) {
        boolean save = eduTeacherService.save(eduTeacher);
        return save ? Result.ok() : Result.fail();
    }

    @ApiOperation(value = "根据ID查询讲师")
    @GetMapping("getTeacher/{id}")
    public Result getTeacher(@ApiParam(name = "id", value = "讲师ID", required = true)
                             @PathVariable String id) {
        EduTeacher eduTeacher = eduTeacherService.getById(id);
        return Result.ok(eduTeacher);
    }

    @ApiOperation(value = "根据ID修改讲师")
    @PostMapping("updateTeacher")
    public Result updateTeacher(@RequestBody EduTeacher eduTeacher) {
        boolean update = eduTeacherService.updateById(eduTeacher);
        return update ? Result.ok() : Result.fail();
    }
}

